package com.baizan.daka

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.util.Log
import android.view.View
import com.baizan.daka.util.ToastUtils
import java.util.regex.Pattern


/**
 * Created by xuhao on 2017/11/14.
 */


fun openApp(context: Context, packageName: String): Boolean {
    var intent = context.getPackageManager().getLaunchIntentForPackage(packageName)
    if (intent != null) {
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
        return true
    }
    return false
}

fun copyContent(context: Context, content: String?) {
    val cmb =
        context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    // 创建普通字符型ClipData
    val mClipData = ClipData.newPlainText("Label", content)
    // 将ClipData内容放到系统剪贴板里。
    cmb.primaryClip = mClipData
}

inline fun <reified T : Activity> Context.startActivity() {
    val intent = Intent(this, T::class.java)
    startActivity(intent)
}


fun Any.showToast(content: String) {
    Log.e("showToast",content)
    ToastUtils.showMShort(content)
}

fun View.dip2px(dipValue: Float): Int {
    val scale = this.resources.displayMetrics.density
    return (dipValue * scale + 0.5f).toInt()
}

fun Context.dip2px(dipValue: Int): Int {
    val scale = this.resources.displayMetrics.density
    return (dipValue * scale + 0.5f).toInt()
}

fun View.px2dip(pxValue: Float): Int {
    val scale = this.resources.displayMetrics.density
    return (pxValue / scale + 0.5f).toInt()
}

fun durationFormat(duration: Long?): String {
    val minute = duration!! / 60
    val second = duration % 60
    return if (minute <= 9) {
        if (second <= 9) {
            "0$minute' 0$second''"
        } else {
            "0$minute' $second''"
        }
    } else {
        if (second <= 9) {
            "$minute' 0$second''"
        } else {
            "$minute' $second''"
        }
    }
}

/**
 * 数据流量格式化
 */
fun Context.dataFormat(total: Long): String {
    var result: String
    var speedReal: Int = (total / (1024)).toInt()
    result = if (speedReal < 512) {
        speedReal.toString() + " KB"
    } else {
        val mSpeed = speedReal / 1024.0
        (Math.round(mSpeed * 100) / 100.0).toString() + " MB"
    }
    return result
}

fun getViewBp(v: View): Bitmap {
    v.isDrawingCacheEnabled = true
    v.buildDrawingCache()
    if (Build.VERSION.SDK_INT >= 11) {
        v.measure(
            View.MeasureSpec.makeMeasureSpec(
                v.width, View.MeasureSpec.EXACTLY
            ), View.MeasureSpec.makeMeasureSpec(
                v.height, View.MeasureSpec.EXACTLY
            )
        )
        v.layout(
            v.x.toInt(), v.y.toInt(),
            v.x.toInt() + v.measuredWidth,
            v.y.toInt() + v.measuredHeight
        )
    } else {
        v.measure(
            View.MeasureSpec.makeMeasureSpec(
                0,
                View.MeasureSpec.UNSPECIFIED
            ),
            View.MeasureSpec.makeMeasureSpec(
                0,
                View.MeasureSpec.UNSPECIFIED
            )
        )
        v.layout(0, 0, v.measuredWidth, v.measuredHeight)
    }
    val b =
        Bitmap.createBitmap(v.drawingCache, 0, 0, v.measuredWidth, v.measuredHeight)
    v.isDrawingCacheEnabled = false
    v.destroyDrawingCache()
    return b
}





